<?php
/**
 * Created by Benjiemin
 * Date: 2020/3/4
 * Time: 16:23
 */

require ('./vendor/autoload.php');
use QL\QueryList;

require ('./aip-php-sdk-2.2.15/AipOcr.php');
 $baseUrl = 'http://www.xmgwbn.com/';
 $name = '小兄弟测试';
 $memberid = time();
 $tel = '1880743'.mt_rand(1111,9999);
 $email = mt_rand(11111111,99999999).'@qq.com';
 $content = '小兄弟测试，验证码太简单，很容易被识别，建议做以下整改：1.提升验证码复杂度，但是这样会导致顾客不方便；2.单个ip每天只能提交一定条数的数据，不然太容易被刷。技术测试，请多包涵。'.time();

    $header = [
        'timeout' => 30,
        'headers' => [
            'Referer' => $baseUrl,
            'Pragma' => 'no-cache',
            'Cache-Control' => 'no-cache',
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
            'Accept' => '*/*',
            'Accept-Encoding' => 'gzip, deflate',
            'Upgrade-Insecure-Requests' => '1',
            'Origin' => $baseUrl,
            'Connection' => 'keep-alive'
        ]
    ];


$time = time();
$i = 0;
while ((time()-$time)<60){
        //进入网页
        $jar = new \GuzzleHttp\Cookie\CookieJar;
        $client = new GuzzleHttp\Client(['cookies' => true]);

        $ql = $client->request('GET', $baseUrl, [
            'cookies' => $jar
        ]);

        if($ql->getStatusCode()!=200){
            echo '网站状态不正常';die;
        }

        $imgUrl = $baseUrl.'Reply-verify-1583928057206';
    //下载验证码到本地
        $ql = $client->request('GET', $imgUrl);
        $imgname = 'code.jpeg';
        file_put_contents($imgname,$ql->getBody(), true);

    //验证码识别
        $appId = '9360309';
        $apiKey = 'f94FyaKX2LKx0GWBoddFVpxa';
        $secretKey = 'oGhM9QqHH6qslErF1LH8etP6Ber5adDz';
        $apiOcrModel = new AipOcr($appId, $apiKey, $secretKey);
        $verifyRet = $apiOcrModel->basicGeneral(file_get_contents($imgname));

        if(!isset($verifyRet['words_result'][0]['words'])){
            echo '识别失败'.PHP_EOL;
            var_dump($verifyRet);
            continue;
        }

        $verify = $verifyRet['words_result'][0]['words'];
    //登录
        $ql = $client->request('POST',$baseUrl.'Reply-add.html',[
            'form_params'=>[
                'name' => $name,
                'memberid' => $memberid,
                'tel'=>$tel,
                'email'=>$email,
                'content'=>$content,
                'verify'=>$verify,
            ]
        ]);
        $i = $i+1;
        echo $i.PHP_EOL;
        sleep(1);
}




